﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace MapSurface.UIModelElement
{
    public class APIModel
    {
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        private List<Widget> contains;

        public List<Widget> Contains
        {
            get { return contains; }
            set { contains = value; }
        }

    }
    public class APIModelManager
    {
        private APIModel _myModel;

        public APIModelManager(string fileName)
        {
            buil(fileName);
        }
        private void buil(string filName)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(filName);
            _myModel = visit(doc.DocumentElement);
        }
        private APIModel visit(XmlElement root)
        {
            if(root.Name.Contains("APIModel"))
            {
                APIModel n = new APIModel(); //apiName
                n.Name= root.Attributes.GetNamedItem("apiName").Value;
                if (root.HasChildNodes)
                {
                    foreach (XmlNode node in root.ChildNodes)
                    {
                        if (node.Name.Contains("contains"))
                        {
                            Widget w = new Widget(node);
                            w.Name = node.Attributes.GetNamedItem("name").Value;
                            w.Node = node;
                            foreach (XmlNode wc in node.ChildNodes)
                            {
                                if (isContainsWidget(wc))
                                {
                                    Widget cw = new Widget(wc);
                                    cw.Name = wc.Attributes.GetNamedItem("name").Value;
                                    if (w.ContainsWidgets == null)
                                        w.ContainsWidgets = cw;
                                    break;
                                }
                            }
                            if (n.Contains == null)
                                n.Contains = new List<Widget>();
                            n.Contains.Add(w);
                        }
                    }
                }
                return n;
            }
            return null;
        }
        public bool isContainsWidget(XmlNode node)
        {
            if (node.Name.Contains("containsWidget"))
                return true;
            return false;
        }

        public bool isContainsWidget(String name)
        {
            foreach (Widget w in this._myModel.Contains)
                if (w.Name.CompareTo(name) != 0)
                    if (w.ContainsWidgets!=null && w.ContainsWidgets.Name.CompareTo(name) == 0)
                        return true;
            return false;
        }


        public Widget findWidget(string name)
        {
            foreach (Widget w in _myModel.Contains)
            {
                if (w.Name.CompareTo(name) == 0)
                    return w;
                else if (w.ContainsWidgets!=null && w.ContainsWidgets.Name.CompareTo(name) == 0)
                    return w;
            }
            return null;
        }
    }

}
